//
//  CAMoneyViewController.m
//  LNH
//
//  Created by twentylx on 2018/1/22.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CAMoneyViewController.h"
#import "CAMoneyHeaderView.h"
#import "CAMoneyTableHeaderView.h"
#import "CAMoneyTableViewCell.h"
#import "CATakeMoneyViewController.h"

@interface CAMoneyViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) CAMoneyHeaderView *headerView;
@property (nonatomic, strong) CAMoneyTableHeaderView *tableHeaderView;
@property (nonatomic, strong) UITableView *moneyTableView;
@property (nonatomic, strong) NSMutableArray *moneyArray;

@end

static NSString *cellIdentifier = @"moneyCell";

@implementation CAMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    NSArray *array = @[
                       @{@"image" : @"sale1"},
                       @{@"image" : @"sale2"},
                       @{@"image" : @"sale3"},
                       @{@"image" : @"sale4"},
                       @{@"image" : @"sale5"},
                       ];
    _moneyArray = [NSMutableArray arrayWithArray:array];
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.moneyTableView];
    
    [self.view setNeedsUpdateConstraints];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"余额";
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(takeAction)];
}

#pragma mark event
- (void) takeAction {
    CATakeMoneyViewController *takeMoneyVC = [CATakeMoneyViewController new];
    [self.navigationController pushViewController:takeMoneyVC animated:YES];
}

#pragma mark layout
- (void) updateViewConstraints {
    [super updateViewConstraints];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.height.mas_equalTo(150);
    }];

    [_moneyTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(self.headerView.mas_bottom);
        make.bottom.equalTo(self.view);
    }];
}

#pragma get & set
- (CAMoneyHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [CAMoneyHeaderView new];
        _headerView.moneyLabel.text = [NSString stringWithFormat:@"当前账户余额：%@", GVUD.money];
    }
    return _headerView;
}

- (CAMoneyTableHeaderView *)tableHeaderView {
    if (!_tableHeaderView) {
        _tableHeaderView = [[CAMoneyTableHeaderView alloc] initWithFrame:(CGRect){0, 0, SCREEN_WIDTH, 100}];
    }
    return _tableHeaderView;
}

- (UITableView *)moneyTableView {
    if (!_moneyTableView) {
        _moneyTableView = [UITableView new];
        _moneyTableView.tableHeaderView = self.tableHeaderView;
        _moneyTableView.dataSource = self;
        _moneyTableView.delegate = self;
        _moneyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_moneyTableView registerClass:[CAMoneyTableViewCell class] forCellReuseIdentifier:cellIdentifier];
    }
    return _moneyTableView;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.moneyArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CAMoneyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    NSDictionary *dict = _moneyArray[indexPath.row];
    cell.titleLabel.text = @"ceshi";
    cell.backgroundColor = [UIColor clearColor];
//    cell.goodsImageView.image = LMImageNamed(@"vip");
//    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

@end
